package com.nikmesoft.android.referendumfriend.service;



import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.nikmesoft.android.referendumfriend.models.user_login;


public class LoginHandler extends DefaultHandler {
	private final static String ERRORCODE_XML = "ErrorCode";
	private final static String ERRORCODE_ID_XML = "errorID";
	private final static String ERRORCODE_MSG_XML = "errorMsg";
	private final static String USER_ID = "id";
	private final static String FULLNAME = "fullname";
	private final static String EMAIL = "email";
	private final static String USERNAME = "username";
	private final static String BIRTHDAY = "birthday";
	private final static String GENDER ="gender";
	private final static String MOBILE = "mobile";
	private final static String AVATAR = "avatar";
	
	private StringBuilder builder;
	public static user_login result=new user_login();
	private ErrorCode errorCode;

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		builder.append(new String(ch, start, length));
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		if (this.errorCode != null) {
			if (localName.equalsIgnoreCase(ERRORCODE_ID_XML)) {
				errorCode.setErrorID(builder.toString().trim());
			} else if (localName.equalsIgnoreCase(ERRORCODE_MSG_XML)) {
				errorCode.setErrorMsg(builder.toString().trim());
			}
		}else if (localName.equalsIgnoreCase(USER_ID)) {
			result.setId(Integer.parseInt(builder.toString().trim()));
		}
		else if (localName.equalsIgnoreCase(FULLNAME)) {
			result.setFullname(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(EMAIL)) {
			result.setEmail(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(USERNAME)) {
			result.setUsername(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(BIRTHDAY)) {
			result.setBirthday(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(GENDER)) {
			result.setGender(Integer.parseInt(builder.toString().trim()));
		}
		else if (localName.equalsIgnoreCase(MOBILE)) {
			result.setMobile(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(AVATAR)) {
			result.setAvatar(builder.toString().trim());
		}
		builder.setLength(0);

	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		builder = new StringBuilder();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if (localName.equalsIgnoreCase(ERRORCODE_XML)) {
			this.errorCode = new ErrorCode();
		}
	}

	public user_login getResult() {
		result.setError(errorCode);
		return result;
	}

}
